﻿using System;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
using System.Text;
using HR.Appointments.DataLayer;

namespace HR.Appointments.Service.DataAccessLayer.DLinq
{
    internal class ReservedTimeDAO : IReservedTimeDAO
    {
        #region IReservedTimeDAO Members
        
        #region Select Methods
        public List<ReservedTimeEntity> GetByDate(int companyId, DateTime date)
        {
            var result = new List<ReservedTimeEntity>();
            using (DAODataContext ctx = new DAODataContext())
            {
                var sql =
                    from data in ctx.ReservedHours
                    join cmp in ctx.Companies on data.ComanyId equals cmp.CompanyId
                    where data.ComanyId == companyId
                        && data.FromDate.CompareTo(date) <= 0 
                        && data.ToDate.CompareTo(date) >= 0
                        && !cmp.EndDate.HasValue
                    orderby data.FromDate, data.FromHour, data.FromMinute, data.ToDate, data.ToHour, data.ToMinute
                    select data;

                foreach (ReservedHour row in sql)
                {
                    result.Add(ReservedTimeEntity.Load(row));
                }
            }
            return result;
        }

        public List<ReservedTimeEntity> GetByCompanyId(int companyId)
        {
            var result = new List<ReservedTimeEntity>();
            using (DAODataContext ctx = new DAODataContext())
            {
                var sql =
                    from data in ctx.ReservedHours
                    join cmp in ctx.Companies on data.ComanyId equals cmp.CompanyId
                    where data.ComanyId == companyId
                        && data.ToDate.CompareTo(DateTime.Today) >= 0
                        && !cmp.EndDate.HasValue
                        orderby data.FromDate, data.FromHour, data.FromMinute, data.ToDate, data.ToHour, data.ToMinute
                    select data;

                foreach (ReservedHour row in sql)
                {
                    result.Add(ReservedTimeEntity.Load(row));
                }
            }
            return result;
        }
        #endregion

        #region Insert, Update, Delete Methods
        public void Insert(ReservedTimeEntity entity)
        {
            var result = new List<ReservedTimeEntity>();
            using (DAODataContext ctx = new DAODataContext())
            {
                ctx.ReservedHours.InsertOnSubmit(entity.ToData(null));
                ctx.SubmitChanges();
            }
        }


		public void Delete(int reservedtimeId)
		{
			using (DAODataContext ctx = new DAODataContext())
			{
				var sql = from data in ctx.ReservedHours
						  where data.Id == reservedtimeId
						  select data;

				if (sql.Count() != 0)
				{
					ctx.ReservedHours.DeleteOnSubmit(sql.FirstOrDefault());
					ctx.SubmitChanges();
				}
			}
        }
        #endregion

        #endregion

    }
}
